﻿using System;
using System.Web.Mvc;
using System.Web.Routing;
using EasySite.Web.Mvc;

namespace EasySite.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            

            routes.MapRoute(
                "Register", // Route name
                "Register", // URL with parameters
                new { controller = "Account", action = "Register" } // Parameter defaults
            );
            routes.MapRoute(
                "Login", // Route name
                "Login", // URL with parameters
                new { controller = "Account", action = "Login" } // Parameter defaults
            );
            routes.MapRoute(
                "Logout", // Route name
                "Logout", // URL with parameters
                new { controller = "Account", action = "Logout" } // Parameter defaults
            );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new {controller = "Home", action = "Index", id = UrlParameter.Optional}, // Parameter defaults
                new {constraint = new DefinedControllerConstraint()}
            );

            routes.MapRoute("AllRoutes",
                            "{*values}",
                            new { controller = "Page", action = "Render" });

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            EasySiteContext.Current.Initialize();
        }

        protected void Application_End()
        {
            EasySiteContext.Current.Dispose();
        }

        protected void Application_Error()
        {
            var ex = Server.GetLastError();
            if (ex != null)
            {
                //todo: log the error
            }
        }

        protected void Application_BeginRequest(object sender, EventArgs args)
        {
            
        }

    }
}